﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Pixysoft.PaymentGateway
{
    public class Tenpay
    {
        private static volatile Tenpay instance;

        private static object syncRoot = new Object();

        public static Tenpay Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new Tenpay();
                    }
                }

                return instance;

            }
        }

        public TenpayRequest CreateRequest(string bargainor_id, string key,
            string billcode, int totalfee, string hostaddress, string return_url)
        {
            if (string.IsNullOrEmpty(bargainor_id))
                return null;

            if (string.IsNullOrEmpty(key))
                return null;

            if (string.IsNullOrEmpty(billcode))
                return null;

            if (billcode.Length > 10)
                billcode = billcode.Substring(billcode.Length - 10, 10);

            if (totalfee < 1)
                return null;

            if (string.IsNullOrEmpty(hostaddress))
                return null;

            if (string.IsNullOrEmpty(return_url))
                return null;

            //当前时间 yyyyMMdd

            string date = DateTime.Now.ToString("yyyyMMdd");

            //商户订单号，不超过32位，财付通只做记录，不保证唯一性

            string sp_billno = billcode;

            //财付通订单号，10位商户号+8位日期+10位序列号，需保证全局唯一

            string transaction_id = bargainor_id + date + billcode;

            //创建PayRequestHandler实例

            PayRequestHandler reqHandler = new PayRequestHandler();

            //设置密钥
            reqHandler.setKey(key);

            //初始化
            reqHandler.init();

            //-----------------------------
            //设置支付参数
            //-----------------------------
            reqHandler.setParameter("bargainor_id", bargainor_id);			//商户号
            reqHandler.setParameter("sp_billno", sp_billno);				//商家订单号
            reqHandler.setParameter("transaction_id", transaction_id);		//财付通交易单号
            reqHandler.setParameter("return_url", return_url);				//支付通知url
            reqHandler.setParameter("desc", "订单号：" + transaction_id);	//商品名称
            reqHandler.setParameter("total_fee", totalfee.ToString());						//商品金额,以分为单位

            //用户ip,测试环境时不要加这个ip参数，正式环境再加此参数
            //reqHandler.setParameter("spbill_create_ip", hostaddress);

            //获取请求带参数的url
            string requestUrl = reqHandler.getRequestURL();

            return new TenpayRequest(requestUrl, transaction_id);
        }

        public TenpayResult GetResponse(HttpContext httpContext, string key)
        {
            if (string.IsNullOrEmpty(key))
                return new TenpayResult(false, null, -1, "用户签名丢失");

            //创建PayResponseHandler实例

            PayResponseHandler resHandler = new PayResponseHandler(httpContext);

            resHandler.setKey(key);

            //判断签名

            if (resHandler.isTenpaySign())
            {
                //交易单号

                string transaction_id = resHandler.getParameter("transaction_id");

                //金额金额,以分为单位

                string total_fee = resHandler.getParameter("total_fee");

                double totalfee = -1;

                if (!double.TryParse(total_fee, out totalfee))
                    return new TenpayResult(false, null, -1, "返回支付金额出错，内部错误！！！");

                //支付结果

                string pay_result = resHandler.getParameter("pay_result");

                if ("0".Equals(pay_result))
                {
                    return new TenpayResult(true, transaction_id, totalfee, "支付成功");
                }
                else
                {
                    return new TenpayResult(false, null, -1, "支付失败");
                }

            }
            else
            {
                return new TenpayResult(false, null, -1, "认证签名失败");
            }
        }

        public void DoSucceedShow(HttpContext httpContext, string showurl)
        {
            //创建PayResponseHandler实例

            PayResponseHandler resHandler = new PayResponseHandler(httpContext);

            resHandler.doShow(showurl);
        }

        private void test()
        {
            CreateRequest("!", "1", "12345678901234567890", 1, "123", "123");
        }
    }

    public class TenpayRequest
    {
        private string requestUrl = null;

        private string tenpayCode = null;

        public TenpayRequest(string requestUrl, string tenpayCode)
        {
            this.requestUrl = requestUrl;

            this.tenpayCode = tenpayCode;
        }
        public string RequestUrl
        {
            get { return requestUrl; }
            set { requestUrl = value; }
        }
        public string Tenpaycode
        {
            get { return tenpayCode; }
            set { tenpayCode = value; }
        }


    }

    public class TenpayResult
    {
        private bool isSucceed = false;

        private string tenpayCode = null;

        private double tenpayFee = -1;

        private string resultMsg;

        public TenpayResult(bool isSucceed, string tenpayCode, double tenpayFee, string resultMsg)
        {
            this.IsSucceed = isSucceed;

            this.tenpayCode = tenpayCode;

            this.tenpayFee = tenpayFee;

            this.resultMsg = resultMsg;
        }

        public bool IsSucceed
        {
            get { return isSucceed; }
            set { isSucceed = value; }
        }
        public string TenpayCode
        {
            get { return tenpayCode; }
            set { tenpayCode = value; }
        }
        public double TenpayFee
        {
            get { return tenpayFee; }
            set { tenpayFee = value; }
        }
        public string ResultMsg
        {
            get { return resultMsg; }
            set { resultMsg = value; }
        }



    }
}
